﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace AsicMonitor
{
    public partial class MinerSetting : Form
    {
        private List<Miner> miners;

        public MinerSetting(List<Miner> miners)
        {
            this.miners = miners;
            if (this.miners == null) this.miners = new List<Miner>();

            InitializeComponent();

            init();
        }

        private void init()
        {
            Win32Utility.SetCueText(minerNameTxt, "请输入矿机名称前缀，不添加时，默认为原名字");
            Win32Utility.SetCueText(addressTxt, "请输入钱包地址");
            Win32Utility.SetCueText(poolAddrTxt, "请输入矿池地址");
            Win32Utility.SetCueText(passTxt, "请输入口令");


            // set algo 
            algoCombox.Items.Clear();
            string selectedAlgo = null;
            foreach(string item in AppConfig.GetAppConfigArray("algo"))
            {
                selectedAlgo = item;
                algoCombox.Items.Add(item);
            }
            foreach(Miner item in miners)
            {
                if(item.Conf != null && item.Conf.algo != null)
                {
                    string algo = item.Conf.algo;
                    if (!algoCombox.Items.Contains(algo))
                    {
                        algoCombox.Items.Add(algo);
                    }
                }
            }
            if(algoCombox.Items.Count == 0)
            {
                algoCombox.Items.Add("lyra2z");
                algoCombox.Items.Add("lyra2rev2");
                selectedAlgo = "lyra2z";
            }
           

            Conf conf = miners[0].Conf;
            selectedAlgo = conf.algo != null ? conf.algo : selectedAlgo;
            if (selectedAlgo != null)
            {
                algoCombox.SelectedItem = selectedAlgo;
            }
            string user = conf.user;
            if(user != null && user.Contains("."))
            {
                user = user.Substring(0, user.IndexOf("."));
            }
            addressTxt.Text = user;
            poolAddrTxt.Text = conf.url;
            passTxt.Text = conf.pass;
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            // config miner
            if(poolAddrTxt.Text == null || poolAddrTxt.Text == "")
            {
                MessageBoxEx.Show(this, "矿池地址不能为空");
                return;
            }
            if(addressTxt.Text == null || addressTxt.Text == "")
            {
                MessageBoxEx.Show(this, "钱包地址不能为空");
                return;
            }
            if(passTxt.Text == null || passTxt.Text == "")
            {
                MessageBoxEx.Show(this, "口令不能为空");
                return;
            }

            Conf conf = new Conf();
            conf.url = poolAddrTxt.Text;
            conf.user = addressTxt.Text;
            conf.pass = passTxt.Text;
            conf.algo = algoCombox.SelectedItem.ToString();
            conf.miner_name = minerNameTxt.Text;

            showProgressBar(conf, miners);
            Close();
        }

        private void showProgressBar(Conf conf, List<Miner> miners)
        {
            SettingProgress progress = new SettingProgress(conf, miners);
            progress.StartPosition = FormStartPosition.CenterParent;
            progress.ShowDialog();
        }

    }
}
